<?php
declare (strict_types = 1);

namespace app\common    \model;

use think\Model;
use think\facade\Db;


/**
 * 企业消息
 * @mixin \think\Model
 */
class Mesg extends Model
{
    public function complists($company_id,$keyword)
    {
        $where = [];
        if ($keyword) {
            $where[] = ['m.name','like',"%$keyword%"];
        }

        $result = $this->alias('g')
            ->join('member m', 'g.member_id = m.id','LEFT')
            ->field('m.id,g.content,m.name,m.avatar,g.create_time,sex')
            ->where('g.company_id',$company_id)
            ->where($where)
            ->group('g.member_id')
            ->order('g.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            $mesg = $this->where('member_id',$value['id'])->where('company_id',$company_id)->order('id desc')->find();

            $result['data'][$key]['create_time'] = strtotime((string) $mesg['create_time']);
            $result['data'][$key]['content'] = $mesg['content'];
        }

        return ['list'=>$result];
    }

    /**
     * 获取聊天记录
     * @return [type] [description]
     */
    public function detail($company_id,$id)
    {
        $ea_company = Db::table('ea_company')->where('id',$company_id)->field('company_name,logo')->find();
        $result = $this->alias('g')
            ->join('member m', 'g.member_id = m.id','LEFT')
            ->field('m.id,g.content,m.name,m.avatar,g.create_time,g.sendtype')
            ->where('g.company_id',$company_id)
            ->where('g.member_id',$id)
            ->select()
            ->toArray();


        foreach ($result as $key => $value) {
            $result[$key]['objective'] = '';
            $result[$key]['create_time'] = strtotime($value['create_time']);
            if ($value['sendtype'] == 1) {
                $result[$key]['obj'] = 1;
                $result[$key]['objective'] = Db::table('ea_objective')->where('member_id',$value['id'])->value('name');
            }
            if ($value['sendtype'] == 2) {
                $result[$key]['obj'] = 2;
                $result[$key]['name'] = $ea_company['company_name'];
                $result[$key]['avatar'] = $ea_company['logo'];
            }
        }

        return ['list'=>$result];
    }

    /**
     * 用户会话列表
     * @return [type] [description]
     */
    public function userlists($member_id,$keyword)
    {
        $where = [];
        if ($keyword) {
            $where[] = ['m.name','like',"%$keyword%"];
        }

        $result = $this->alias('g')
            ->join('company c', 'g.company_id = c.id')
            ->field('c.id,g.content,c.company_name as name,c.logo avatar,g.create_time,g.company_id')
            ->where('g.member_id',$member_id)
            ->where($where)
            ->group('g.company_id')
            ->order('g.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            $mesg = $this->where('member_id',$member_id)->where('company_id',$value['id'])->order('id desc')->find();

            $result['data'][$key]['create_time'] = strtotime($mesg['create_time']);
            $result['data'][$key]['content'] = $mesg['content'];
        }

        return ['list'=>$result];
    }

    /**
     * 获取聊天记录
     * @return [type] [description]
     */
    public function userdetail($company_id,$id)
    {
        $logo = Db::table('ea_company')->where('id',$company_id)->value('logo');
        $result = $this->alias('g')
            ->join('member m', 'g.member_id = m.id','LEFT')
            ->field('g.id,g.content,m.name,m.avatar,g.create_time,g.sendtype,g.company_id')
            ->where('g.company_id',$company_id)
            ->where('g.member_id',$id)
            ->select()
            ->toArray();


        foreach ($result as $key => $value) {
            $result[$key]['create_time'] = strtotime($value['create_time']);
            if ($value['sendtype'] == 2) {
                $result[$key]['avatar'] = $logo;
                $result[$key]['obj'] = 2;
            } else {
                $result[$key]['obj'] = 1;
            }
        }

        return ['list'=>$result];
    }

    /**
     * 用户职位投递
     * @return [type] [description]
     */
    public function userjob($member_id)
    {
        $result = Db::table('ea_mesg')->alias('m')
                ->join('company c', 'm.company_id = c.id','LEFT')
                ->join('job j', 'm.job_id = j.id','LEFT')
                ->where('m.member_id',$member_id)
                ->where('m.sendtype',1)
                ->where('m.type',1)
                ->field('j.id,j.title as name,j.company_id,c.company_name,j.salary,j.academic_requirements,j.experience_requirement,j.address,c.area_no,j.status')
                ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            $result['data'][$key]['area_no'] = areaNoText($value['area_no']); // 上班地区
            $result['data'][$key]['salary'] = salary($value['salary']);

            $result['data'][$key]['academic_requirements'] = academic_requirements($value['academic_requirements']);  // 学历要求
            $result['data'][$key]['experience_requirement'] = experience_requirement($value['experience_requirement']);  //  经验要求

            $management = Db::table('ea_company_management c')
                ->join('ea_member m','c.member_id = m.id')
                ->where('c.company_id',$value['company_id'])
                ->field('m.name,position,avatar,sex')
                ->find();

            if ($management) {
                $result['data'][$key]['personnel_name'] = $management['name'] ? $management['name'] : '招聘员';
                $result['data'][$key]['position'] = $management['position'] ? $management['position'] : '人事部';
            }

        }

        return ['list'=>$result];
    }
}
